package afcp.alumni.model.db;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;

import org.apache.commons.lang3.text.WordUtils;

import afcp.alumni.model.Pb;
import afcp.alumni.model.db.Member.Sex;

import com.google.code.geocoder.Geocoder;
import com.google.code.geocoder.GeocoderRequestBuilder;
import com.google.code.geocoder.model.GeocodeResponse;
import com.google.code.geocoder.model.GeocoderRequest;
import com.google.code.geocoder.model.GeocoderResult;
import com.google.code.geocoder.model.GeocoderStatus;
import com.google.code.geocoder.model.LatLng;
import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat;
import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;

public class MemberUtils {

	// TODO voir si on peut obtenir ces noms par réflexion de Member.
	public static final String COL_LASTNAME = "lastName";
	public static final String COL_FIRSTNAME = "firstName";
	public static final String COL_SEX = "sex";
	public static final String COL_CHINESE_NAME = "chineseName";
	public static final String COL_BIRTH_DATE = "birthDate";
	public static final String COL_PARISTECH_ENTRANCE_YEAR = "parisTechEntranceYear";
	public static final String COL_PARISTECH_SCHOOL = "parisTechSchool";
	public static final String COL_CHINESE_UNIVERSITY = "chineseUniversity";
	public static final String COL_SCHOLARSHIP = "scholarship";
	public static final String COL_DOMAIN_IN_FRANCE = "domainInFrance";
	public static final String COL_DOMAIN_IN_CHINA = "domainInChina";
	public static final String COL_OTHER_DIPLOMA = "otherDiploma";
	public static final String COL_DIPLOMA_UNIV = "diplomaUniversity";
	public static final String COL_FIRST_JOB_COUNTRY = "firstJobCountry";
	public static final String COL_INTERNSHIP = "internship";
	public static final String COL_FIRST_JOB = "firstJob";
	public static final String COL_JOB_COUNTRY = "jobCountry";
	public static final String COL_EMPLOYER = "employer";
	public static final String COL_EMAIL = "email";
	public static final String COL_PHONE = "phone";
	public static final String COL_ADDRESS_SUPPLEMENT = "addressSupplement";
	public static final String COL_ADDRESS = "address";
	public static final String COL_COUNTRY = "country";
	public static final String COL_WEIBO = "weibo";
	public static final String COL_WEIXIN = "weixin";
	public static final String COL_NOTES = "notes";

	
	// Valeurs utiles seulement à l'export de fichier Excel a priori.
	private static final String LASTNAME_VAL = "Nom 姓（拼音）";
	private static final String FIRSTNAME_VAL = "Prénom 名（拼音）";
	private static final String SEX_VAL = "Sex 性别";
	private static final String CHINESE_NAME_VAL = "Nom Chinois 中文姓名";
	private static final String BIRTH_DATE_VAL = "Date de naissance 出生日期";
	private static final String PARISTECH_ENTRANCE_YEAR_VAL = "Année d'entrée à ParisTech 入学巴黎高科年份";
	private static final String PARISTECH_SCHOOL_VAL = "Ecole ParisTech 所属巴黎高科学校";
	private static final String CHINESE_UNIVERSITY_VAL = "Université en Chine 所属国内大学";
	private static final String SCHOLARSHIP_VAL = "Bourse 奖学金";
	private static final String DOMAIN_IN_FRANCE_VAL = "Domaine d'études en France 在法学习专业领域";
	private static final String DOMAIN_IN_CHINA_VAL = "Domaine d'études en Chine 国内学习专业领域";
	private static final String OTHER_DIPLOMA_VAL = "Autres diplômes 其他学位";
	private static final String DIPLOMA_UNIV_VAL = "Université/ école du diplôme 该学位授予学校";
	private static final String FIRST_JOB_COUNTRY_VAL = "Pays du 1er emploi 第一个正式工作所在国";
	private static final String INTERNSHIP_VAL = "Stage 实习公司";
	private static final String FIRST_JOB_VAL = "1er emploi (si vous travaillez déjà) 第一个正式工作雇主 （如果已经有正式工作经历）";
	private static final String JOB_COUNTRY_VAL = "Pays de l'emploi actuel 现工作所在国";
	private static final String EMPLOYER_VAL = "Employeur actuelle 现雇主";
	private static final String EMAIL_VAL = "Email 电子邮箱";
	private static final String PHONE_VAL = "Téléphone 电话";
	private static final String ADDRESS_SUPPLEMENT_VAL = "Complément d'adresse";
	private static final String ADDRESS_VAL = "Adresse 居住地址";
	private static final String COUNTRY_VAL = "Pays de résidance 居住国";
	private static final String WEIBO_VAL = "WeiBo （微博）";
	private static final String WEIXIN_VAL = "WeiXin (微信)";
	private static final String NOTES_VAL = "Remarques: 备注:";

//	public static final String ROLE_ADMIN = "ADMIN";
//	public static final String ROLE_ADMIN_LABEL = "Administrateur";
//	public static final String ROLE_MEMBER = "MEMBER";
//	public static final String ROLE_MEMBER_LABEL = "Membre";
//	
//	public static final String[] ROLES = new String[] { ROLE_ADMIN, ROLE_MEMBER };
	

	public static final FileColumn[] DEFAULT_COLUMNS = new FileColumn[] {
			new FileColumn(COL_LASTNAME, 1, LASTNAME_VAL),
			new FileColumn(COL_FIRSTNAME, 2, FIRSTNAME_VAL),
			new FileColumn(COL_SEX, 3, SEX_VAL),
			new FileColumn(COL_CHINESE_NAME, 4, CHINESE_NAME_VAL),
			new FileColumn(COL_BIRTH_DATE, 5, BIRTH_DATE_VAL),
			new FileColumn(COL_PARISTECH_ENTRANCE_YEAR, 6, PARISTECH_ENTRANCE_YEAR_VAL),
			new FileColumn(COL_PARISTECH_SCHOOL, 7, PARISTECH_SCHOOL_VAL),
			new FileColumn(COL_CHINESE_UNIVERSITY, 8, CHINESE_UNIVERSITY_VAL),
			// 9 : Bourse
			new FileColumn(COL_SCHOLARSHIP, 9, SCHOLARSHIP_VAL),
			// 10 : Domaine d'étude en France
			new FileColumn(COL_DOMAIN_IN_FRANCE, 10, DOMAIN_IN_FRANCE_VAL),
			// 11 : Domaine d'étude en Chine
			new FileColumn(COL_DOMAIN_IN_CHINA, 11, DOMAIN_IN_CHINA_VAL),
			// 12 : Autres diplômes
			new FileColumn(COL_OTHER_DIPLOMA, 12, OTHER_DIPLOMA_VAL),
			// 13 : université/école du diplôme
			new FileColumn(COL_DIPLOMA_UNIV, 13, DIPLOMA_UNIV_VAL),
			new FileColumn(COL_FIRST_JOB_COUNTRY, 14, FIRST_JOB_COUNTRY_VAL),
			// 15 : stage
			new FileColumn(COL_INTERNSHIP, 15, INTERNSHIP_VAL),
			new FileColumn(COL_FIRST_JOB, 16, FIRST_JOB_VAL),
			// 17 : pays de l'emploi actuel
			new FileColumn(COL_JOB_COUNTRY, 17, JOB_COUNTRY_VAL),
			new FileColumn(COL_EMPLOYER, 18, EMPLOYER_VAL),
			new FileColumn(COL_EMAIL, 19, EMAIL_VAL, true),
			new FileColumn(COL_PHONE, 20, PHONE_VAL),
			// 21 : adresse
			new FileColumn(COL_ADDRESS_SUPPLEMENT, 21, ADDRESS_SUPPLEMENT_VAL),
			new FileColumn(COL_ADDRESS, 22, ADDRESS_VAL),
			// 22 : pays de résidence
			new FileColumn(COL_COUNTRY, 23, COUNTRY_VAL),
			new FileColumn(COL_WEIBO, 24, WEIBO_VAL),
			new FileColumn(COL_WEIXIN, 25, WEIXIN_VAL),
			// 25 : remarques
			new FileColumn(COL_NOTES, 26, NOTES_VAL),
			
//			new CsvColumn(COL_LOGIN, LOGIN_VAL, LOGIN_VAL),
//			new CsvColumn(COL_PASSWORD, PASSWORD_VAL, PASSWORD_VAL),
			};
	
	public static class ColumnNumToValueConvertor {

		private int lastCol;
		
		private int LASTNAME;
		private int FIRSTNAME;
		private int SEX;
		private int CHINESE_NAME;
		private int BIRTH_DATE;
		private int PARISTECH_ENTRANCE_YEAR;
		private int PARISTECH_SCHOOL;
		private int CHINESE_UNIVERSITY;
		private int SCHOLARSHIP;
		private int DOMAIN_IN_FRANCE;
		private int DOMAIN_IN_CHINA;
		private int OTHER_DIPLOMA;
		private int DIPLOMA_UNIV;
		private int FIRST_JOB_COUNTRY;
		private int INTERNSHIP;
		private int FIRST_JOB;
		private int JOB_COUNTRY;
		private int EMPLOYER;
		private int EMAIL;
		private int PHONE;
		private int ADDRESS_SUPPLEMENT;
		private int ADDRESS;
		private int COUNTRY;
		private int WEIBO;
		private int WEIXIN;
		private int NOTES;

		public ColumnNumToValueConvertor(List<FileColumn> columns) {
			int pos;
			for (FileColumn column : columns) {
				pos = column.getPosition() - 1;
				if (pos > lastCol) {
					lastCol = pos;
				}
				switch (column.getId()) {
				case COL_LASTNAME:
					LASTNAME = pos;
					break;
				case COL_FIRSTNAME:
					FIRSTNAME = pos;
					break;
				case COL_SEX:
					SEX = pos;
					break;
				case COL_CHINESE_NAME:
					CHINESE_NAME = pos;
					break;
				case COL_BIRTH_DATE:
					BIRTH_DATE = pos;
					break;
				case COL_PARISTECH_ENTRANCE_YEAR:
					PARISTECH_ENTRANCE_YEAR = pos;
					break;
				case COL_PARISTECH_SCHOOL:
					PARISTECH_SCHOOL = pos;
					break;
				case COL_CHINESE_UNIVERSITY:
					CHINESE_UNIVERSITY = pos;
					break;
				case COL_SCHOLARSHIP:
					SCHOLARSHIP = pos;
					break;
				case COL_DOMAIN_IN_FRANCE:
					DOMAIN_IN_FRANCE = pos;
					break;
				case COL_DOMAIN_IN_CHINA:
					DOMAIN_IN_CHINA = pos;
					break;
				case COL_OTHER_DIPLOMA:
					OTHER_DIPLOMA = pos;
					break;
				case COL_DIPLOMA_UNIV:
					DIPLOMA_UNIV = pos;
					break;
				case COL_FIRST_JOB_COUNTRY:
					FIRST_JOB_COUNTRY = pos;
					break;
				case COL_INTERNSHIP:
					INTERNSHIP = pos;
					break;
				case COL_FIRST_JOB:
					FIRST_JOB = pos;
					break;
				case COL_JOB_COUNTRY:
					JOB_COUNTRY = pos;
					break;
				case COL_EMPLOYER:
					EMPLOYER = pos;
					break;
				case COL_EMAIL:
					EMAIL = pos;
					break;
				case COL_PHONE:
					PHONE = pos;
					break;
				case COL_ADDRESS_SUPPLEMENT:
					ADDRESS_SUPPLEMENT = pos;
					break;
				case COL_ADDRESS:
					ADDRESS = pos;
					break;
				case COL_COUNTRY:
					COUNTRY = pos;
					break;
				case COL_WEIBO:
					WEIBO = pos;
					break;
				case COL_WEIXIN:
					WEIXIN = pos;
					break;
				case COL_NOTES:
					NOTES = pos;
					break;
				}
			}
		}
		
		public Object get(int i, Member member) {
			
			if (i == LASTNAME) {
				return member.getLastName();
			} else if (i == FIRSTNAME) {
				return member.getFirstName();
			} else if (i == SEX) {
				return member.getSex();
			} else if (i == CHINESE_NAME) {
				return member.getChineseName();
			} else if (i == BIRTH_DATE) {
				return member.getBirthDate();
			} else if (i == PARISTECH_ENTRANCE_YEAR) {
				return member.getParisTechEntranceYear();
			} else if (i == PARISTECH_SCHOOL) {
				return member.getParisTechSchool();
			} else if (i == CHINESE_UNIVERSITY) {
				return member.getChineseUniversity();
			} else if (i == SCHOLARSHIP) {
				return member.getScholarship();
			} else if (i == DOMAIN_IN_FRANCE) {
				return member.getDomainInFrance();
			} else if (i == DOMAIN_IN_CHINA) {
				return member.getDomainInChina();
			} else if (i == OTHER_DIPLOMA) {
				return member.getOtherDiploma();
			} else if (i == DIPLOMA_UNIV) {
				return member.getDiplomaUniversity();
			} else if (i == FIRST_JOB_COUNTRY) {
				return member.getFirstJobCountry();
			} else if (i == INTERNSHIP) {
				return member.getInternship();
			} else if (i == FIRST_JOB) {
				return member.getFirstJob();
			} else if (i == JOB_COUNTRY) {
				return member.getJobCountry();
			} else if (i == EMPLOYER) {
				return member.getEmployer();
			} else if (i == EMAIL) {
				return member.getEmail();
			} else if (i == PHONE) {
				return member.getPhone();
			} else if (i == ADDRESS_SUPPLEMENT) {
				return member.getAddressSupplement();
			} else if (i == ADDRESS) {
				return member.getAddress();
			} else if (i == COUNTRY) {
				return member.getCountry();
			} else if (i == WEIBO) {
				return member.getWeibo();
			} else if (i == WEIXIN) {
				return member.getWeixin();
			} else if (i == NOTES) {
				return member.getNotes();
				
				
			} else {
				return null;
			}
		}
		
//		public void set(int i, Member member, String cellValue) {
//			set(i, member, cellValue, null);
//		}
		
		public void set(int i, Member member, String cellValue, String cellFormat) {
			
			if (i == LASTNAME) {
				setLastName(member, cellValue);
			} else if (i == FIRSTNAME) {
				setFirstName(member, cellValue);
			} else if (i == SEX) {
				setSex(member, cellValue);
			} else if (i == CHINESE_NAME) {
				setChineseName(member, cellValue);
			} else if (i == BIRTH_DATE) {
				setBirthDate(member, cellValue, cellFormat);
			} else if (i == PARISTECH_ENTRANCE_YEAR) {
				setParisTechEntranceYear(member, cellValue);
			} else if (i == PARISTECH_SCHOOL) {
				setParisTechSchool(member, cellValue);
			} else if (i == CHINESE_UNIVERSITY) {
				setChineseUniversity(member, cellValue);
			} else if (i == SCHOLARSHIP) {
				setScholarship(member, cellValue);
			} else if (i == DOMAIN_IN_FRANCE) {
				setDomainInFrance(member, cellValue);
			} else if (i == DOMAIN_IN_CHINA) {
				setDomainInChina(member, cellValue);
			} else if (i == OTHER_DIPLOMA) {
				setOtherDiploma(member, cellValue);
			} else if (i == DIPLOMA_UNIV) {
				setDiplomaUniversity(member, cellValue);
			} else if (i == FIRST_JOB_COUNTRY) {
				setFirstJobCountry(member, cellValue);
			} else if (i == INTERNSHIP) {
				setInternship(member, cellValue);
			} else if (i == FIRST_JOB) {
				setFirstJob(member, cellValue);
			} else if (i == JOB_COUNTRY) {
				setJobCountry(member, cellValue);
			} else if (i == EMPLOYER) {
				setEmployer(member, cellValue);
			} else if (i == EMAIL) {
				setEmail(member, cellValue);
			} else if (i == PHONE) {
				setPhone(member, cellValue);
			} else if (i == ADDRESS_SUPPLEMENT) {
				setAddressSupplement(member, cellValue);
			} else if (i == ADDRESS) {
				setAddress(member, cellValue);
			} else if (i == COUNTRY) {
				setCountry(member, cellValue);
			} else if (i == WEIBO) {
				setWeibo(member, cellValue);
			} else if (i == WEIXIN) {
				setWeixin(member, cellValue);
			} else if (i == NOTES) {
				setNotes(member, cellValue);
				
				
			}
		}
		
		public int getLastCol() {
			return lastCol;
		}
	}
	
	public static void setAll(Member member, String lastName, String firstName, String sex,
			String chineseName, String birthDate, String birthDateFormat, String parisTechEntranceYear,
			String parisTechSchool, String chineseUniversity,
			String scholarship, String domainInFrance, String domainInChina,
			String otherDiploma, String diplomaUniversity,
			String firstJobCountry, String internship, String firstJob,
			String jobCountry, String employer, String email, String phone,
			String addressSupplement, String address, String gps, String country, String weibo, String weixin,
			String notes) {

		setLastName(member, lastName);
		setFirstName(member, firstName);
		setSex(member, sex);
		setChineseName(member, chineseName);
		setBirthDate(member, birthDate, birthDateFormat);
		setParisTechEntranceYear(member, parisTechEntranceYear);
		setParisTechSchool(member, parisTechSchool);
		setChineseUniversity(member, chineseUniversity);
		setScholarship(member, scholarship);
		setDomainInFrance(member, domainInFrance);
		setDomainInChina(member, domainInChina);
		setOtherDiploma(member, otherDiploma);
		setDiplomaUniversity(member, diplomaUniversity);
		setFirstJobCountry(member, firstJobCountry);
		setInternship(member, internship);
		setFirstJob(member, firstJob);
		setJobCountry(member, jobCountry);
		setEmployer(member, employer);
		setEmail(member, email);
		setPhone(member, phone);
		setAddressSupplement(member, addressSupplement);
		setAddress(member, address);
		setCountry(member, country);
		setWeibo(member, weibo);
		setWeixin(member, weixin);
		setNotes(member, notes);

	}
	
	/**
	 * {@link #DEFAULT_COLUMNS} as {@link List}. Shorthand for <code>Arrays.asList(Member.DEFAULT_COLUMNS)</code>.
	 * @return
	 */
	public static List<FileColumn> defaultColumns() {
		List<FileColumn> columns = new LinkedList<FileColumn>();
		for (FileColumn column : DEFAULT_COLUMNS) {
			columns.add(column.copy());
		}
		return columns;
//		return Arrays.asList(DEFAULT_COLUMNS);
	}
	
	public static Map<String, FileColumn> defaultColumnsMap() {
		Map<String, FileColumn> columns = new HashMap<>();
		for (FileColumn column : DEFAULT_COLUMNS) {
			columns.put(column.getId(), column.copy());
		}
		return columns;
	}

	
	private static boolean empty(String str) {
		return str == null || str.isEmpty();
	}
	
	// Data parsing methods
	
	// last name
	
	public static void setLastName(Member member, String cellValue) {
		if (cellValue != null) {
			member.setLastName(cellValue.toUpperCase());
		}
	}
	
	// first name
	
	private static final char[] nameDelimiters = new char[] {' ', '-', '\''};
	public static void setFirstName(Member member, String cellValue) {
		member.setFirstName(WordUtils.capitalizeFully(cellValue, nameDelimiters));
	}
	
	// Sex
	
	private static final Set<String> HOMME = new HashSet<String>();
	private static final Set<String> FEMME = new HashSet<String>();
	static {
		HOMME.add("homme");
		HOMME.add("h");
		HOMME.add("m");
		FEMME.add("femme");
		FEMME.add("f");
	}
	public static Member.Sex toSex(String sex) {
		if (sex == null) {
			return null;
		}
		String lowerCaseSex = sex.toLowerCase();
		if (HOMME.contains(lowerCaseSex)) {
			return Member.Sex.MALE;
		} else if (FEMME.contains(lowerCaseSex)) {
			return Member.Sex.FEMALE;
		} else {
			return null;
		}
	}
	public static String sexToString(Sex sex) {
		switch (sex) {
		case MALE:
			return "Homme";
		case FEMALE:
			return "Femme";
		default:
			return "";
		}
	}
	public static void setSex(Member member, String cellValue) {
		Sex sex = toSex(cellValue);
		if (cellValue != null && sex == null) {
			member.addWarning(Pb.INCORRECT_SEX);
		}
		member.setSex(sex);
	}
	
	public static void setChineseName(Member member, String chineseName) {
		member.setChineseName(chineseName);
	}
	
	// Birth date
	
	private static final String FORMAT = "dd/MM/yyyy";
	private static final DateFormat dateFormat = new SimpleDateFormat(FORMAT, Locale.FRENCH);
	public static Date toDate(String date) throws ParseException {
		return toDate(date, dateFormat);
	}
	public static Date toDate(String date, String format) throws ParseException {
		try {
			return toDate(date, new SimpleDateFormat(format));
		} catch (IllegalArgumentException e) {
			return null;
		}
	}
	public static Date toDate(String date, DateFormat dateFormat) throws ParseException {
		if (date == null) {
			return null;
		}
		// TODO améliorer la détection des formats de date :
		// http://stackoverflow.com/questions/4024544/how-to-parse-dates-in-multiple-formats-using-simpledateformat
		// ou google : java detect date format
//		try {
			return dateFormat.parse(date);
//		} catch (ParseException e) {
//			logger.warn("Couldn't parse birth date \"" + date + "\". Expected format: " + FORMAT);
//			return null;
//		}
	}
	public static String toString(Date date) {
		if (date == null) {
			return null;
		} else {
			return dateFormat.format(date);
		}
	}
	public static TimeZone getTimeZone() {
		return dateFormat.getTimeZone();
	}
	public static void setBirthDate(Member member, String cellValue, String formatString) {
		if (!empty(cellValue)) {
			Date birthDate;
			try {
				if (formatString != null) {
					birthDate = toDate(cellValue,
							formatString.replace('m', 'M'));
				} else {
					birthDate = toDate(cellValue);
				}
			} catch (ParseException e) {
				birthDate = null;
			}
			if (birthDate == null) {
				member.addWarning(Pb.INCORRECT_BIRTHDATE_FORMAT);
			}
			member.setBirthDate(birthDate);
		}
	}
	
	
	// Year
	
	/**
	 * Convert {@link String} to year as <code>int</code>.
	 * 
	 * @param year
	 * @return the year if correct, or {@link Member#NULL_YEAR_VALUE} if <code>year</code> is
	 *         <code>null</code> or {@link Member#INCORRECT_YEAR_NUMBER_FORMAT} if
	 *         <code>year</code> is not a valid <code>int</code>.
	 */
	public static int toYear(String year) throws NullPointerException,
			NumberFormatException {
		if (year == null) {
			throw new NullPointerException();
		}
		if (year.indexOf('.') != -1) {
			return (int) Double.parseDouble(year);
		} else {
			return Integer.parseInt(year);
		}
	}
	public static void setParisTechEntranceYear(Member member, String cellValue) {
		int parisTechEntranceYear = -1;
		try {
			parisTechEntranceYear = toYear(cellValue);
		} catch (NullPointerException e) {
			member.addWarning(Pb.NULL_YEAR_VALUE);
		} catch (NumberFormatException e) {
			member.addWarning(Pb.INCORRECT_YEAR_NUMBER_FORMAT);
		}
		member.setParisTechEntranceYear(parisTechEntranceYear);
	}

	public static void setParisTechSchool(Member member, String parisTechSchool) {
		member.setParisTechSchool(parisTechSchool);
	}

	public static void setChineseUniversity(Member member,
			String chineseUniversity) {
		member.setChineseUniversity(chineseUniversity);
	}

	public static void setScholarship(Member member, String scholarship) {
		member.setScholarship(scholarship);
	}

	public static void setDomainInFrance(Member member, String domainInFrance) {
		member.setDomainInFrance(domainInFrance);
	}

	public static void setDomainInChina(Member member, String domainInChina) {
		member.setDomainInChina(domainInChina);
	}

	public static void setOtherDiploma(Member member, String otherDiploma) {
		member.setOtherDiploma(otherDiploma);
	}

	public static void setDiplomaUniversity(Member member,
			String diplomaUniversity) {
		member.setDiplomaUniversity(diplomaUniversity);
	}

	public static void setFirstJobCountry(Member member, String firstJobCountry) {
		member.setFirstJobCountry(firstJobCountry);
	}

	public static void setInternship(Member member, String internship) {
		member.setInternship(internship);
	}

	public static void setFirstJob(Member member, String firstJob) {
		member.setFirstJob(firstJob);
	}

	public static void setJobCountry(Member member, String jobCountry) {
		member.setJobCountry(jobCountry);
	}

	public static void setEmployer(Member member, String employer) {
		member.setEmployer(employer);
	}
	
	// email
	
	public static void setEmail(Member member, String email) {
//		if (empty(cellValue)) {
//			member.addError(Pb.NO_EMAIL);
//		} else {
			member.setEmail(email);
//		}
	}
	
	// Phone
	
	private static final String DEFAULT_REGION = "FR";
	private static final String CHINESE_REGION = "CN";
	private static final PhoneNumberUtil PHONE_UTIL = PhoneNumberUtil
			.getInstance();
	
	public static void setPhone(Member member, String cellValue) {
		if (!empty(cellValue)) {
			try {
				PhoneNumber phone = PHONE_UTIL.parse(cellValue, DEFAULT_REGION);
				boolean valid = PHONE_UTIL.isValidNumber(phone);
				if (!valid && cellValue.indexOf('+') != 0) {
					phone = PHONE_UTIL.parse("+" + cellValue, DEFAULT_REGION);
					valid = PHONE_UTIL.isValidNumber(phone);
					if (!valid) {
						phone = PHONE_UTIL.parse(cellValue, CHINESE_REGION);
						valid = PHONE_UTIL.isValidNumber(phone);
						if (!valid) {
							phone = PHONE_UTIL.parse("+" + cellValue, CHINESE_REGION);
							valid = PHONE_UTIL.isValidNumber(phone);
						}
					}
				}
				String out;
				if (valid) {
					out = PHONE_UTIL.format(phone, PhoneNumberFormat.INTERNATIONAL);
				} else {
					member.addWarning(Pb.UNPARSABLE_PHONE_NUMBER);
					out = cellValue;
				}
				member.setPhone(out);
			} catch (NumberParseException e) {
				member.addWarning(Pb.UNPARSABLE_PHONE_NUMBER);
			}
		}
	}
	
	public static void setAddressSupplement(Member member, String addressSupplement) {
		member.setAddressSupplement(addressSupplement);
	}
	
	// address
	
	// TODO ajouter des champs dans le membre : coordonnées GPS et adresse dans 2/3 langues.
	// Tester la traduction d'adresse en javascript, ce qui évite de les stocker et permet de ne pas
	// se limiter aux langues prévues à l'indexation (utilise la langue de l'utilisateur).
	
	public static void setAddress(Member member, String cellValue) {
		if (empty(cellValue)) {
			return;
		}
		String newAddress = cellValue;
		final Geocoder geocoder = new Geocoder();
//		GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setAddress("Paris, France").setLanguage("en").getGeocoderRequest();
		GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setAddress(cellValue)/*.setLanguage("fr")*/.getGeocoderRequest();
		try {
			GeocodeResponse geocoderResponse = geocoder.geocode(geocoderRequest);
			if (geocoderResponse.getStatus() != GeocoderStatus.OK) {
				member.addWarning(Pb.ADDRESS_PARSING_ERROR);
			} else {
				List<GeocoderResult> results = geocoderResponse.getResults();
				int size = results.size();
				if (size == 0) {
					member.addWarning(Pb.UNPARSABLE_ADDRESS);
				} else {
					if (size > 1) {
						member.addWarning(Pb.SEVERAL_ADDRESSES_FOUND);
					}
					GeocoderResult result = results.get(0);
					LatLng location = result.getGeometry().getLocation();
					member.setGpsLat(location.getLat().doubleValue());
					member.setGpsLong(location.getLng().doubleValue());
					newAddress = result.getFormattedAddress();
				}
			}
			
		} catch (IOException e) {
			member.addWarning(Pb.UNPARSABLE_ADDRESS);
		}
		member.setAddress(newAddress);
	}
	

	public static void setCountry(Member member, String country) {
		member.setCountry(country);
	}

	public static void setWeibo(Member member, String weibo) {
		member.setWeibo(weibo);
	}

	public static void setWeixin(Member member, String weixin) {
		member.setWeixin(weixin);
	}

	public static void setNotes(Member member, String notes) {
		member.setNotes(notes);
	}

}
